package com.project.biz.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.biz.CmsRecoveryOrderProBiz;
import com.project.biz.CmsRecoveryTypeBiz;
import com.project.entity.CmsRecoveryOrderPro;
import com.project.entity.CmsRecoveryType;
import com.project.mapper.CmsRecoveryOrderProMapper;
import com.project.pojo.model.AddRecoveryOrderProModel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 回收订单商品业务层接口实现类
 * @Author: zhuchi
 * @CreateDate: 2020/11/13 21:40
 * @Version: 1.0
 */
@Service
public class CmsRecoveryOrderProBizImpl extends ServiceImpl<CmsRecoveryOrderProMapper, CmsRecoveryOrderPro> implements CmsRecoveryOrderProBiz {

    /**
     * 回收品类业务层接口类
     */
    @Autowired
    private CmsRecoveryTypeBiz recoveryTypeBiz;

    /**
     * 根据回收订单ID获取回收订单商品
     * @param recoveryOrderId
     * @return
     */
    public List<CmsRecoveryOrderPro> getRecoveryOrderPro(Long recoveryOrderId){
        List<CmsRecoveryOrderPro> recoveryOrderPros = new ArrayList<>();
        QueryWrapper<CmsRecoveryOrderPro> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("recovery_order_id", recoveryOrderId);

        List<CmsRecoveryOrderPro> list = this.baseMapper.selectList(queryWrapper);
        if (null != list && list.size() > 0){
            recoveryOrderPros = list;
        }

        return recoveryOrderPros;
    }

    /**
     * 添加回收订单商品
     * @param recoveryOrderProModels
     * @param recoveryOrderId
     * @return
     */
    public int addRecoveryOrderPro(List<AddRecoveryOrderProModel> recoveryOrderProModels, Long recoveryOrderId){
        int result = 0;
        for (AddRecoveryOrderProModel recoveryOrderProModel : recoveryOrderProModels){
            CmsRecoveryOrderPro recoveryOrderPro = new CmsRecoveryOrderPro();
            //获取用户选择的回收品类信息
            CmsRecoveryType recoveryType = this.recoveryTypeBiz.getById(recoveryOrderProModel.getRecoveryTypeId());
            BeanUtils.copyProperties(recoveryType, recoveryOrderPro);

            recoveryOrderPro.setRecoveryOrderId(recoveryOrderId);
            recoveryOrderPro.setSellNumber(recoveryOrderProModel.getSellNumber());

            int addProState = this.baseMapper.insert(recoveryOrderPro);
            if (addProState > 0){
                result++;
            }
        }

        return result;
    }

}
